package com.lablzh.gdxy.component;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Iterator;

@Component
public class UserDetailsUtil {


    public String getUserDetails() {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Collection<? extends GrantedAuthority> authorities = ((UserDetails) principal).getAuthorities();
        return ((UserDetails) principal).getUsername();
    }

    public String getUserRole() {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Iterator<? extends GrantedAuthority> it = ((UserDetails) principal).getAuthorities().iterator();
        String authority = null;
        if (it.hasNext()) {
            authority = it.next().getAuthority();
        }
        return authority;
    }

}
